﻿namespace USElections.CandidatePage.Pages
{
    using System;
    using System.Windows.Media;
    using AgFx;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using USElections.Model;

    /// <summary>
    /// Page1 class.
    /// </summary>
    public partial class CandidatePage : PhoneApplicationPage
    {
        #region Constructor
        /// <summary>
        /// Initializes a new instance of the Page1 class.
        /// </summary>
        public CandidatePage()
        {
            InitializeComponent();
        }
        #endregion

        #region Public methods
        /// <summary>
        /// On Navigated To.
        /// </summary>
        /// <param name="e">The NavigationEventArgs object.</param>
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            Candidate candidate = null;

            try
            {
                int id = Convert.ToInt32(NavigationContext.QueryString[Model.Enums.Variables.CandidateId.ToString()]);
                candidate = Data.Instance.Candidates.GetCandidateById(id);
            }
            catch
            {
            }

            if (candidate == null)
            {
                // error occurred.
                if (NavigationService.CanGoBack)
                {
                    NavigationService.GoBack();
                }
                else
                {
                    this.NavigationService.Navigate(new Uri("/USElections;component/Pages/MainPage.xaml", UriKind.Relative));
                }
            }
            else
            {
                this.DataContext = candidate;

                if (!string.IsNullOrEmpty(candidate.TwitterAccount))
                {
                    // show pivot item, load data.
                    this.twitterPanorama.Visibility = System.Windows.Visibility.Visible;
                    this.panaromaCtrl.DefaultItem = this.twitterPanorama;
                    SystemTray.ProgressIndicator.IsVisible = true;

                    try
                    {
                        DataManager.Current.Load<TwitterLibrary.TwitterFeed>(
                            new TwitterLibrary.TwitterLoadContext(candidate.TwitterAccount),
                            (vm) =>
                            {
                                if (vm != null)
                                {
                                    this.twitterGrid.DataContext = vm;
                                    SystemTray.ProgressIndicator.IsVisible = false;
                                }
                                else
                                {
                                    SystemTray.ProgressIndicator.IsVisible = false;
                                    this.notification.Show("Failed to refresh twitter feed", new SolidColorBrush(Colors.Red), null);
                                }
                            },
                            (vm) =>
                            {
                                SystemTray.ProgressIndicator.IsVisible = false;
                                this.notification.Show("Failed to refresh twitter feed", new SolidColorBrush(Colors.Red), null);
                            });
                    }
                    catch
                    {
                        SystemTray.ProgressIndicator.IsVisible = false;
                        this.notification.Show("Failed to refresh twitter feed", new SolidColorBrush(Colors.Red), null);
                    }
                }
                else
                {
                    this.twitterPanorama.Visibility = System.Windows.Visibility.Collapsed;
                    this.twitterGrid.DataContext = null;
                }
            }
        }
        #endregion
    }
}